
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for group_comments.
	''' </summary>
	Public Partial Class group_comments
		Inherits BaseWebPage
		Protected Button1 As System.Web.UI.WebControls.Button
		Private grpID As Integer = -1
		Protected grp As Group = Nothing
		Protected usr As User = Nothing
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.FRIENDS_COMMENTS_PAGE;

			' Auto login from an external link
			'AutoLogin( Request );

			Me.initPageTitles(Constants.GROUP_COMMENTS_PAGE, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			Try
				If LoggedUser = Nothing Then
					Common.Utils.RedirectToLoginPage(Page)
				End If

				usr = LoggedUser
				If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
					grp = Groups.GetGroupByID(grpID)
				End If
				If grp = Nothing Then
					Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
				End If

				'if (grp.Uid != usr.ID)
				'	throw new Exception (Constants.ERROR_MESSAGE_ONLY_MODERATOR_ACCESS);

				Dim action As String = ""
				If Request.Params("action") <> Nothing AndAlso Request.Params("action") <> "" Then
					action = Request.Params("action")
				End If

				' init "No comments" label

				Dim NewComments As ArrayList = grp.NewComments
				Dim ApprovedComments As ArrayList = grp.Comments
				Dim nCommentsCount As Double = NewComments.Count + ApprovedComments.Count

				If NewComments.Count = 0 AndAlso ApprovedComments.Count = 0 Then
					dgMessages.Visible = False
					lblNoComments.Visible = True
					trPagerUp.Visible = False
					trPager.Visible = False
				Else
					dgPager.RedirectPage = Constants.GROUP_COMMENTS_PAGE

					dgPager.BeforePageNumberText = ""
					dgPagerUp.BeforePageNumberText = ""

					If action.Length > 0 AndAlso LoggedUser.ID = grp.Uid Then
						dgPager.RedirectPage += "?" + Constants.GROUP_PARAM + "=" + grp.ID + "&" + "action=" + action
					Else
						dgPager.RedirectPage += "?" + Constants.GROUP_PARAM + "=" + grp.ID
					End If
					dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(nCommentsCount / Constants.MAX_USER_COMMENTS_ON_COMMENTS_PAGE))
					dgPager.Action = "-1"
					dgPagerUp.RedirectPage = Constants.GROUP_COMMENTS_PAGE
					If action.Length > 0 AndAlso LoggedUser.ID = grp.Uid Then
						dgPagerUp.RedirectPage += "?" + Constants.GROUP_PARAM + "=" + grp.ID + "&" + "action=" + action
					Else
						dgPagerUp.RedirectPage += "?" + Constants.GROUP_PARAM + "=" + grp.ID
					End If
					dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(nCommentsCount / Constants.MAX_USER_COMMENTS_ON_COMMENTS_PAGE))
					dgPagerUp.Action = "-1"

					' initialize listing literals
					dgPager.dgListing.TotalCount = Convert.ToInt32(nCommentsCount)
					dgPagerUp.dgListing.TotalCount = Convert.ToInt32(nCommentsCount)
					'initialize current page number
					If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
						dgPager.CurrentPageNumber = 1
						dgPagerUp.CurrentPageNumber = 1
						ViewState("StartIndex") = 0
					Else
						dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
						dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
						ViewState("StartIndex") = (dgPager.CurrentPageNumber - 1) * Constants.MAX_USER_COMMENTS_ON_COMMENTS_PAGE
					End If

					dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
					dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
					dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
					dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				End If

				hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
				hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)

				'bind comments
				If Not Page.IsPostBack Then
					BindGrid()
				End If

				' set Group Comments
				litWho.Text = grp.Name + "'s Group Comments"
			Catch ex As Exception
				lblNoComments.Visible = True
				'lblNoComments.Text = this.GetPageConstText(htPageConstants, "CONST_Error_TEXT");
				lblNoComments.Text = ex.Message
				trPager.Visible = False
				trPagerUp.Visible = False
			End Try
		End Sub
		Protected Function CreateDataSource() As ICollection
			Dim action As String = ""
			If Request.Params("action") <> Nothing AndAlso Request.Params("action") <> "" Then
				action = Request.Params("action")
			End If

			Dim dt As New DataTable()
			Dim curCommentDate As DateTime

			Dim newCommentList As ArrayList, approvedCommentList As ArrayList, allComments As ArrayList
			allComments = New ArrayList()
			newCommentList = grp.NewComments
			approvedCommentList = grp.Comments
			If grp.Uid = LoggedUser.ID Then
				allComments = newCommentList
			End If
			Dim i As Integer = 0
			While i < approvedCommentList.Count
				allComments.Add(approvedCommentList(i))
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While

			If allComments.Count <= 0 Then
				dgPager.FirstPageNumber = 1
				dgPager.LastPageNumber = 1
				dgPager.MaxPageNumber = 1
				dgPager.PagesRest = 1
				dgPager.dgListing.StartIndex = 0
				dgPager.dgListing.EndIndex = 0
				dgPagerUp.FirstPageNumber = 1
				dgPagerUp.LastPageNumber = 1
				dgPagerUp.MaxPageNumber = 1
				dgPagerUp.PagesRest = 1
				dgPagerUp.dgListing.StartIndex = 0
				dgPagerUp.dgListing.EndIndex = 0
				dgMessages.AllowSorting = False
			Else
				Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
				Dim endIndex As Integer = startIndex + Constants.MAX_USER_COMMENTS_ON_COMMENTS_PAGE
				Dim t As Integer = 0
				Dim counter As Integer = 0

				endIndex = Math.Min(endIndex, allComments.Count)
				t = (endIndex - startIndex)
				dgPager.dgListing.StartIndex = startIndex + 1
				dgPager.dgListing.EndIndex = endIndex
				dgPagerUp.dgListing.StartIndex = startIndex + 1
				dgPagerUp.dgListing.EndIndex = endIndex

				' initialize listing literals
				dgPager.dgListing.TotalCount = Convert.ToInt32(allComments.Count)
				dgPager.dgListing.UpdateControlsTexts()
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(allComments.Count)
				dgPagerUp.dgListing.UpdateControlsTexts()

				' List group's comments

				Dim dr As DataRow
				dt.Columns.Add(New DataColumn("SenderID", GetType(Integer)))
				dt.Columns.Add(New DataColumn("SenderName", GetType(String)))
				dt.Columns.Add(New DataColumn("SenderImageURL", GetType(String)))
				dt.Columns.Add(New DataColumn("OnlineImg", GetType(String)))
				dt.Columns.Add(New DataColumn("MemberURL", GetType(String)))
				dt.Columns.Add(New DataColumn("Date", GetType(String)))
				dt.Columns.Add(New DataColumn("Time", GetType(String)))
				dt.Columns.Add(New DataColumn("Comment", GetType(String)))
				dt.Columns.Add(New DataColumn("CommentID", GetType(String)))
				dt.Columns.Add(New DataColumn("ButtonsVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("RejectText", GetType(String)))
				dt.Columns.Add(New DataColumn("SenderNameFull", GetType(String)))
				dt.Columns.Add(New DataColumn("RemoveVisibility", GetType(Boolean)))
				dt.Columns.Add(New DataColumn("SenderFullName", GetType(String)))

				Dim LoggedUsersList As ArrayList = Common.Utils.GetLoggedUsersIDs()

				Dim temp As UserPicture
				Dim curSender As User
				' List group's NEW comments
				For Each grpComment As GroupComment In allComments
					If counter >= startIndex AndAlso counter < endIndex Then
						dr = dt.NewRow()
						curCommentDate = grpComment.[Date]
						curSender = Users.GetUserByID(grpComment.PostedByUserID)
						dr(0) = grpComment.PostedByUserID
						dr(1) = getBeginning(HttpUtility.HtmlEncode(curSender.Name), Constants.MAX_NAME_LENGTH_ABOVE_IMAGE)
						dr(11) = curSender.Name
						temp = curSender.DefaultPicture
						If temp <> Nothing Then
							dr(2) = Pictures.GetPictureSrcFullURL(temp, True, "../")
						Else
							dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
						End If

						If LoggedUsersList.Contains(curSender.ID) Then
							dr(3) = "../images/online_status.gif"
						Else
							dr(3) = "../images/spacer.gif"
						End If

						'if(curSender.Status == Users.STATUS_ACTIVE)
						If curSender.Status <> Users.STATUS_LOCKED Then
							dr(4) = "../" + Constants.MEMBERS + "?id=" + grpComment.PostedByUserID
						End If

						dr(5) = curCommentDate.DayOfWeek + ", " + Constants.ARR_MONTHS(curCommentDate.Month - 1) + " " + curCommentDate.Day + ", " + curCommentDate.Year
						dr(6) = curCommentDate.ToShortTimeString()
						dr(7) = grpComment.Comment
						dr(8) = grpComment.ID

						If grpComment.CommentStatus = GroupComment.Status.ucsNew Then
							dr(9) = True
							dr(10) = "Reject"
						Else
							dr(9) = False
							'if(action == Constants.FRIENDS_COMMENTS_EDIT_ACTION)
							dr(10) = "Remove"
						End If
						If grp.Uid = LoggedUser.ID OrElse LoggedUser.ID = grpComment.PostedByUserID Then
							dr(12) = True
						Else
							dr(12) = False
						End If
						dt.Rows.Add(dr)
					End If
					System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
				Next
			End If
			Dim dv As New DataView(dt)
			Return dv
		End Function
		Protected Sub BindGrid()
			dgMessages.DataSource = CreateDataSource()
			dgMessages.DataBind()
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.dgMessages.ItemCommand, AddressOf Me.dgMessages_ItemCommand

		End Sub
#End Region

		Private Sub dgMessages_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
			Dim lbPushed As LinkButton = DirectCast(e.CommandSource, LinkButton)
			Dim cmd As String = e.CommandName
			Select Case cmd
				Case "Reject"
					' reject/remove comment
					grp.RemoveComment(Convert.ToInt32(e.CommandArgument))
					Exit Select
				Case "Approve"
					'approve comment
					grp.ApproveComment(Convert.ToInt32(e.CommandArgument))
					Exit Select
			End Select
			BindGrid()
		End Sub


		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






